package com.jy.utils;

import com.jy.domain.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception {

        //获取uri
        String uri = req.getRequestURI();
        StringBuffer url = req.getRequestURL();
//        System.out.println(uri);
//        System.out.println(url);
        //登录逻辑放过
        if(uri.contains("login")){
            return true;
        }
        //用户是否登录
        User user = (User) req.getSession().getAttribute("user"); //强制转换
        if(user!=null){   //用户已经是登录状态了
            return true;
        }else{   //否则跳转到登录页面
            req.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(req, res);
        }
        return false;
    }
}


